unit UCadCentro;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Modelo, DB, DBTables, Grids, DBGrids, StdCtrls, Buttons,
  CheckLst, ExtCtrls, Mask, DBCtrls, UDPTOUteis;

type
  TFCadCentro = class(TFModelo)
    qPadraoCEN_DESCRICAO: TStringField;
    qPadraoARE_CODIGO: TIntegerField;
    qPadraoSUBARE_CODIGO: TIntegerField;
    qPadraoCEN_CODIGO: TIntegerField;
    lbl1: TLabel;
    dbedt1: TDBEdit;
    lbl2: TLabel;
    dbedt2: TDBEdit;
    lbl3: TLabel;
    dbedt3: TDBEdit;
    lbl4: TLabel;
    dbedt4: TDBEdit;
    EdDescSubArea: TEdit;
    EdDescArea: TEdit;
    procedure dbedt3DblClick(Sender: TObject);
    procedure dbedt3Exit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadCentro: TFCadCentro;

implementation

uses UPesqSubArea;

{$R *.dfm}

procedure TFCadCentro.dbedt3DblClick(Sender: TObject);
begin
  inherited;
  dbedt3.Text := DPTOChamaPesquisa(FPesqSubArea,TFPesqSubArea,'SUBARE_CODIGO', QPadrao, dbedt3.Text);
end;

procedure TFCadCentro.dbedt3Exit(Sender: TObject);
begin
  inherited;
   if dbedt3.Text <> '' then
   begin
        if ((qPadrao.state <> dsEdit) and (qPadrao.State <> dsInsert)) then
             qPadrao.Edit;

        qGenerica.Close;
        qGenerica.SQL.Clear;

        qGenerica.SQL.Add('Select SUBARE_DESCRICAO, ARE_CODIGO FROM SUB_AREA WHERE SUBARE_CODIGO = ' + QuotedStr(dbedt3.Text));
        qGenerica.Open;

        EdDescSubArea.Text := qGenerica.fieldByName('SUBARE_DESCRICAO').AsString;
        //
        dbedt2.Text := qGenerica.fieldByName('ARE_CODIGO').AsString;
        qGenerica.Close;
        qGenerica.SQL.Clear;

        qGenerica.SQL.Add('Select ARE_DESCRICAO FROM AREA WHERE ARE_CODIGO = ' + QuotedStr(dbedt2.Text));
        qGenerica.Open;

        EdDescArea.Text := qGenerica.fieldByName('ARE_DESCRICAO').AsString;


        // CALCULA O CODIGO DA SUBAREA JA COM BASE NA AREA
        qGenerica.Close;
        qGenerica.SQL.Clear;

        qGenerica.SQL.Add('Select MAX(CEN_CODIGO) as MAXCOD_CENTRO FROM CENTRO WHERE ARE_CODIGO = ' + QuotedStr(dbedt2.Text) + ' and  SUBARE_CODIGO = ' + QuotedStr(dbedt3.Text) );
        qGenerica.Open;

        qPadrao.FieldByName('CEN_CODIGO').AsInteger := qGenerica.fieldByName('MAXCOD_CENTRO').AsInteger + 1;

        dbedt1.SetFocus;
        
        qGenerica.Close;
        qGenerica.Free;


   end;

end;

end.
